package com.qfedu.edu.controller.thymeleaf;


import com.qfedu.edu.pojo.User;
import com.qfedu.edu.service.impl.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.List;


@Controller
public class UserThymeleafController {
    @Autowired
    private UserService userService;

    @GetMapping("/thymeleaf/toUserListPage")
    public String toUserListPage() {
        return "redirect:/thymeleaf/list";
    }

    @GetMapping("/thymeleaf/toUserAddPage")
    public String toUserAddPage() {
        return "user_add";

    }

    @PostMapping("/thymeleaf/addUser")
    public String addUser(User user) {
        userService.addUser(user);
        return "redirect:/thymeleaf/list";
    }

    @GetMapping("/thymeleaf/list")
    public String list(Model model) {
        List<User> users = userService.findAll();
        model.addAttribute("userList", users);
        return "user_list";
    }

    @GetMapping("/thymeleaf/deleteById")
    public String deleteById(Integer id) {
        userService.deleteById(id);
        return "redirect:/thymeleaf/list";
    }

    @GetMapping("/thymeleaf/listById")
    public String listById(Integer id, Model model) {
        User user = userService.findUserById(id);
        model.addAttribute("user", user);
        return "user_edit";
    }

    @PostMapping("/thymeleaf/updateUser")
    public String updateUser(User user) {
        userService.updateUser(user);
        return "redirect:/thymeleaf/list";
    }
}
